size=5;
location=1;
rgb = imread('b1(1).bmp');%读取原图像
I = 255 - rgb2gray (rgb) ; %转化为灰度图像
figure; subplot (121) %显示灰度图像
imshow (I)
text (732, 501, '原始图像', ...
'FontSize', 7, 'HorizontalAlignment' , ' right')
hy = fspecial('sobel') ;%sobel算子
hx = hy';
Iy = imfilter(double(I),hy,'replicate');%滤波求y方向边缘
Ix = imfilter(double(I),hx,'replicate');%滤波求x方向边缘
gradmag = sqrt(Ix.^2 + Iy.^2);%求模
subplot(122) ; imshow (gradmag, []),%显示梯度
title ('Gradient magnitude (gradmag)')

L = watershed (gradmag) ;%直接应用分水岭算法
Lrgb = label2rgb(L) ;%转化为彩色图像.
figure; imshow(Lrgb), %显示分割后的图像
title( 'Watershed transform of gradient magnitude (Lrgb) ')

se = strel('disk', 10) ; %圆形结构元素
Io = imopen(I,se) ;%形态学开操作
figure; subplot(121);
imshow(Io),% 显示执行开操作后的图像
title('Opening (Io)')
Ie = imerode(I, se);%对图像进行腐蚀
Iobr = imreconstruct(Ie,I) ;%形态学重建
subplot (122); imshow(Iobr), %显示重建后的图像
title ('Opening-by-reconstruction (Iobr) ')
Ioc=imclose(Io,se);%形态学关操作
figure; subplot(121)
imshow(Ioc),%显示关操作后的图像
title('opening-closing (Ioc) ')
Iobrd = imdilate(Iobr, se) ; %对图像进行膨胀
Iobrcbr = imreconstruct(imcomplement (Iobrd),imcomplement(Iobr)) ;%形态学重建
Iobrcbr = imcomplement(Iobrcbr);%图像求反
subplot(122); imshow(Iobrcbr),%显示重建求反后的图像
title ('opening-closing by reconstruction (Iobrcbr) ')
fgm = imregionalmax(Iobrcbr) ; %局部极大值
figure; imshow(fgm), %显示重建后局部极大值图像
title ('Regional maxima of opening-closing by reconstruction (fgm) ')
I2=I;
I2(fgm) = 255;%局部极大值处像素值设置为255
figure; imshow(I2),%在原图上显示极大值区域
title (' Regional maxima superimposed on original image (I2) ')
se2 = strel (ones(5,5));%结构元素
fgm2 = imclose(fgm,se2);%关操作.
fgm3 = imerode (fgm2, se2) ; %腐蚀
fgm4 = bwareaopen (fgm3, 20) ;%开操作
I3=I;
I3 (fgm4) = 255;%前景处设置为255
figure; subplot (121)
imshow (I3) %显示修改后的极大值区域
title ( 'Modified regional maxima')
bw = im2bw (Iobrcbr, graythresh (Iobrcbr)) ; %转化为二值图像
subplot (122); imshow(bw), %显示二值图像
title ('Thresholded opening-closing by reconstruction')

D = bwdist(bw) ;%计算距离
DL = watershed(D) ;%分水岭变换
bgm = DL == 0;%取分割边界
figure; imshow(bgm),%显示分割后的边界
title('Watershed ridge lines (bgm)')
gradmag2 = imimposemin (gradmag, bgm | fgm4) ;%设置最小值
L = watershed(gradmag2) ; %分水岭变换
I4 = I;
I4(imdilate(L == 0,ones(3, 3)) | bgm | fgm4) = 255; %前景及边界处设置为255
figure; subplot(121)
imshow(I4) %突出前景及边界
title('Markers and object boundaries')
Lrgb = label2rgb(L,'jet',[.5,.5,.5]);%转化为伪彩色图像
subplot (122); imshow(Lrgb);%显示伪彩色图像
title('Colored watershed label matrix')
figure; imshow(I) ,
hold on
himage = imshow(Lrgb); %在原图上显示伪彩色图像
set (himage,'AlphaData', 0.3) ;
title('Lrgb superimposed transparently on original image')


